// 引入模块
const express = require('express');
// 开启路由
const r = express.Router();
//  链接数据库
const pool = require('../pool.js');

// 测试链接
r.get('/cs',(req,res)=>{
	res.send('order服务器成功开启')
});

// 添加地址
r.post("/v1/addressAdd",(req,res)=>{
	var obj = req.body;
	sql = 'insert into xz_receiver_address set ?'
	pool.query(sql,[obj],(err,result)=>{
		if(err) throw err;
		if(result.affectedRows===0){
			res.send("0");
		}else{
			res.send("1");
		}
	});
});

// 修改地址
r.put("/v1/addressUpdate",(req,res)=>{
     var obj = req.body;

	sql = 'UPDATE xz_receiver_address SET ? WHERE aid = ?';
	pool.query(sql,[obj,obj.aid],(err,result)=>{
		if(err) throw err;
		if(result.affectedRows===0){
			res.send("0");
		}else{
			res.send("1");
		}
	});
});

// 删除地址
r.delete("/v1/del/:uid",(req,res)=>{
	var $aid = req.params.aid;
	
	sql = "delete from xz_receiver_address where aid=?"
	pool.query(sql,[$aid],(err,result)=>{
		if(err) throw err;
		if(result.affectedRows===0){
			res.send("0");
		}else{
			res.send("1");
		}
	});
});

// 索引查询地址
r.get("/v1/addressSearch/:aid",(req,res)=>{
	var $aid = req.params.aid;

	sql = "select * from xz_receiver_address where aid=?"
	pool.query(sql,[$aid],(err,result)=>{
		if(err) throw err;
		if(result.length>0){
			res.send(result);
		}else{
			res.send("0");
		}
	});
});

// 分页查询地址
r.get("/v1/addressList/:page&:count",(req,res)=>{
	var $page = req.params.page;
	var $count = req.params.count;
	
	sql = "select * from xz_receiver_address limit ?,?"
	pool.query(sql,[$page,$count],(err,result)=>{
		if(err) throw err;
		if(result.length>0){
			res.send(result);
		}else{
			res.send("0");
		}
	});
});











// 文件最后导出路由
module.exports = r;
